﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace WpfApplication1.Classes
{
    public class MessageUtils
    {
        public static String getTimerCheckMessage(int id) //巡测命令
        {
            String idStr = getHexId(id);
            String msg = String.Format("88 00 6{0} {1}2 0A 00 01 00 00 00 00 00 00", idStr.Substring(0, 1), idStr.Substring(1, 1));
            return msg;
        }

        public static String getCardCheckSuccessMessage(int id) //验卡成功
        {
            String idStr = getHexId(id);
            String msg = String.Format("88 00 6{0} {1}8 07 00 04 4F 00 00 00 55 00", idStr.Substring(0, 1), idStr.Substring(1, 1));
            return msg;
        }
        public static String getCardCheckErrorMessage(int id) //验卡失败
        {
            String idStr = getHexId(id);
            String msg = String.Format("88 00 6{0} {1}8 07 00 04 4F 00 00 00 AA 00", idStr.Substring(0, 1), idStr.Substring(1, 1));
            return msg;
        }

        public static List<String> getSendBillInfoMessage(int id, String orderCode, double shouldNum, String carNo) //下发提单信息
        {
            List<String> cmds = new List<String>();

            String idStr = getHexId(id);

            String headerStr = String.Format("88 00 6{0} {1}8 04 ", idStr.Substring(0,1), idStr.Substring(1,1));

            String shouldNumStr = shouldNum.ToString("0000000.0");

            String cmd01 = String.Format("40 {0} {1} {2} {3} 0A {4} {5}"
                , orderCode.Substring(0, 2), orderCode.Substring(2, 2)
                , orderCode.Substring(4, 2), orderCode.Substring(6, 2)
                , shouldNumStr.Substring(0, 2), shouldNumStr.Substring(2, 2)
                );
            String cmd02 = String.Format("81 {0} {1}{2} {3} {4} {5} {6} {7}"
                , shouldNumStr.Substring(4, 2), shouldNumStr.Substring(6, 1), shouldNumStr.Substring(8, 1)
                , carNo.Substring(0, 2), carNo.Substring(2, 2)
                , carNo.Substring(4, 2), carNo.Substring(6, 2), carNo.Substring(8, 2)
                );
            carNo = carNo.PadRight(10, '0');
            String cmd03 = String.Format("C0 {0} {1} {2} 01 00 00 00"
                , carNo.Substring(10, 2), carNo.Substring(12, 2)
                , carNo.Substring(14, 2)
                );

            cmds.Add(headerStr + cmd01);
            cmds.Add(headerStr + cmd02);
            cmds.Add(headerStr + cmd03);

            return cmds;
        }
        public static List<String> getClearBillInfoMessage(int id, String orderCode) //下发提单信息
        {
            List<String> cmds = new List<String>();

            String idStr = getHexId(id);
            String headerStr = String.Format("88 00 6{0} {1}8 05 ", idStr.Substring(0, 1), idStr.Substring(1, 1));

            String cmd01 = String.Format("00 {0} {1} {2} {3} 44 00 00"
                , orderCode.Substring(0, 2), orderCode.Substring(2, 2)
                , orderCode.Substring(4, 2), orderCode.Substring(6, 2)
                );

            cmds.Add(headerStr + cmd01);

            return cmds;
        }

        public static List<String> getRecordAckMessage(int id, String orderCode) //发油记录应答
        {
            List<String> cmds = new List<String>();

            String idStr = getHexId(id);
            String headerStr = String.Format("88 00 6{0} {1}8 02 ", idStr.Substring(0, 1), idStr.Substring(1, 1));

            String cmd01 = String.Format("00 00 {0} {1} {2} {3} 81 45"
                , orderCode.Substring(0, 2), orderCode.Substring(2, 2)
                , orderCode.Substring(4, 2), orderCode.Substring(6, 2)
                );

            cmds.Add(headerStr + cmd01);

            return cmds;
        }

        private static String getHexId(int id)
        {
            id = (id) * 2 + 1;
            return ConvertHex(id.ToString()).PadLeft(2, '0');
        }

        public static string ConvertHex(string value)
        {
            string sReturn = string.Empty;
            try
            {
 
                while (ulong.Parse(value) >= 16)
                {
                    ulong v = ulong.Parse(value);
                    sReturn = GetHexChar((v % 16).ToString()) + sReturn;
                    value = Math.Floor(Convert.ToDouble(v / 16)).ToString();
                }
                sReturn = GetHexChar(value) + sReturn;
            }
            catch
            {
                sReturn = "###Valid Value!###";
            }
            return sReturn;
        }

        public static string GetHexChar(string value)
        {
            string sReturn = string.Empty;
            switch (value)
            {
                case "10":
                    sReturn = "A";
                    break;
                case "11":
                    sReturn = "B";
                    break;
                case "12":
                    sReturn = "C";
                    break;
                case "13":
                    sReturn = "D";
                    break;
                case "14":
                    sReturn = "E";
                    break;
                case "15":
                    sReturn = "F";
                    break;
                default:
                    sReturn = value;
                    break;
            }
            return sReturn;
        }

        public static float getFloatFromHex(String hexStr)
        {
            try
            {
                MatchCollection matches = Regex.Matches(hexStr, @"[0-9A-Fa-f]{2}");
                byte[] bytes = new byte[matches.Count];
                for (int i = 0; i < bytes.Length; i++)
                    bytes[i] = byte.Parse(matches[i].Value, System.Globalization.NumberStyles.AllowHexSpecifier);
                float m = BitConverter.ToSingle(bytes.Reverse().ToArray(), 0);
                
                return m;
            }catch(Exception ex)
            {
                return 0.0F;
            }
        }

        public static String getHexFromFloat(float n)
        {
            try
            {
                var b = BitConverter.GetBytes(n);
                String hex = BitConverter.ToString(b.Reverse().ToArray()).Replace("-", "");

                return hex;
            }catch(Exception ex)
            {
                return "00000000";
            }
        }
    }

    
}
